package second.codeTop.package1;

import util.ListNode;

/**
 * @ClassName LC206
 * @description: 反转链表
 * @author: 荣燊
 * @create: 2022-03-01 15:40
 **/
public class LC206 {
    // 递归法
    public static ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode node = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return node;
    }



    // 双指针法
    public static ListNode reverseList1(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        ListNode next = null;

        while (cur != null) {
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
}
